package com.smalls.newvideotwo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.MeasureUtil;
import com.smalls.newvideotwo.utils.ViewUtil;

public class SearchGridview extends BaseGridView {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "SearchGridview":"SearchGridview";
    private Runnable downRunable = new Runnable() {
        public void run() {
            if (SearchGridview.this.getSelectedItemPosition() + 8 < SearchGridview.this.getCount()) {
                SearchGridview.this.setSelection(SearchGridview.this.getSelectedItemPosition() + 8);
            }
            SearchGridview.this.isDownfilling = false;
        }
    };
    private int halfHeight;
    private boolean isDownfilling = false;
    private boolean isUpfilling = false;
    private Runnable upRunable = new Runnable() {
        public void run() {
            if (SearchGridview.this.getSelectedItemPosition() - 8 >= 0) {
                SearchGridview.this.setSelection(SearchGridview.this.getSelectedItemPosition() - 8);
            }
            SearchGridview.this.isUpfilling = false;
        }
    };
    private int verheight = 8;

    public SearchGridview(Context context) {
        super(context);
        this.verheight = (int) MeasureUtil.getHeightSize(ViewUtil.DipToPixels(context, 8.0f));
        Log.d(this.TAG, "1verheight=" + this.verheight);
    }

    public SearchGridview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.verheight = (int)MeasureUtil.getHeightSize(ViewUtil.DipToPixels(context, 8.0f));
        Log.d(this.TAG, "2verheight=" + this.verheight);
    }

    public SearchGridview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.verheight =(int)MeasureUtil.getHeightSize(ViewUtil.DipToPixels(context, 8.0f));
        Log.d(this.TAG, "3verheight=" + this.verheight);
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
            if (event.getAction() != 0 || this.isUpfilling) {
                return true;
            }
            this.isUpfilling = true;
            if (getItemCurrentRow() || getSelectedItemPosition() < 7) {
                this.isUpfilling = false;
                return super.dispatchKeyEvent(event);
            }
            smoothScrollBy(-((getHeight() + (this.verheight * 2)) / 2), 500);
            Log.d(this.TAG, "verheight=" + this.verheight + "getHeight()=" + getHeight() + "--" + ((getHeight() - this.verheight) / 2));
            postDelayed(this.upRunable, 500);
            return true;
        } else if (event.getKeyCode() != KeyEvent.KEYCODE_DPAD_DOWN) {
            if (event.getKeyCode() != 21 && event.getKeyCode() == 22) {
            }
            return super.dispatchKeyEvent(event);
        } else if (event.getAction() != 0 || this.isDownfilling) {
            return true;
        } else {
            this.isDownfilling = true;
            if (!getItemCurrentRow() || getSelectedItemPosition() > getCount() - 9) {
                this.isDownfilling = false;
                return super.dispatchKeyEvent(event);
            }
            Log.d(this.TAG, "");
            smoothScrollBy((getHeight() - this.verheight) / 2, 500);
            postDelayed(this.downRunable, 400);
            return true;
        }
    }

    public boolean getItemCurrentRow() {
        if (getSelectedView() == null || getSelectedView().getBottom() <= getHalfHeigh()) {
            return false;
        }
        return true;
    }

    private int getHalfHeigh() {
        if (this.halfHeight > 0) {
            return this.halfHeight;
        }
        WindowManager manager = (WindowManager)NewDTVApp.getApp().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);
        this.halfHeight = dm.heightPixels / 2;
        return this.halfHeight;
    }
}
